package com.example.travelcomponentdemo;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

public class UsingBlowfish extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		run();
	}
	
	private final static String ALGORITM = "Blowfish";
	private final static String KEY = "ixA83j!1";
	private final static String IV = "gurunavi";
	private final static String PLAIN_TEXT = "ima_nomo@1344246764";
	private final static String result = "k9lHdajenUekad52nKDF38H7iAqqlhx470Sw3js_FLg";


	public void run() {

		try {

			byte[] encrypted = encrypt(KEY, PLAIN_TEXT);
			Log.i("FOO", "Encrypted: " + bytesToHex(encrypted));

			
//			String decrypted = decrypt(KEY, encrypted);
//			Log.i("FOO", "Decrypted: " + decrypted);

		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		}
	}

	private byte[] encrypt(String key, String plainText) throws GeneralSecurityException {
		byte[] IVBytes = IV.getBytes();
		IvParameterSpec iv = new IvParameterSpec(IVBytes);
		   
		SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), ALGORITM);

		Cipher cipher = Cipher.getInstance(ALGORITM + "/CBC/ZeroBytePadding");
		cipher.init(Cipher.ENCRYPT_MODE, secret_key, iv);

		return cipher.doFinal(plainText.getBytes());
	}

	private String decrypt(String key, byte[] encryptedText) throws GeneralSecurityException {

		SecretKey secret_key = new SecretKeySpec(key.getBytes(), ALGORITM);

		Cipher cipher = Cipher.getInstance(ALGORITM);
		cipher.init(Cipher.DECRYPT_MODE, secret_key);

		byte[] decrypted = cipher.doFinal(encryptedText);

		return new String(decrypted);
	}

	public static String bytesToHex(byte[] data) {

		if (data == null)
			return null;

		String str = "";

		for (int i = 0; i < data.length; i++) {
			if ((data[i] & 0xFF) < 16)
				str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
			else
				str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
		}

		return str;

	}
/////////////////////////////////////////////////////////////////////////////////////
	private static byte[] convertToEncrypt(String str)
			throws UnsupportedEncodingException {
		byte[] byteEncrypt = str.getBytes("UTF-8");
		int lengthEncrypt = 8 - (byteEncrypt.length % 8);
		String sp = new String(hexToBytes("00"));
		for (int i = 0; i < lengthEncrypt; i++) {
			str += sp;
		}
		byteEncrypt = str.getBytes("UTF-8");
		return byteEncrypt;
	}
	
	public static byte[] hexToBytes(String str) {
		if (str == null) {
			return null;
		} else if (str.length() < 2) {
			return null;
		} else {
			int len = str.length() / 2;
			byte[] buffer = new byte[len];
			for (int i = 0; i < len; i++) {
				buffer[i] = (byte) Integer.parseInt(
						str.substring(i * 2, i * 2 + 2), 16);
			}
			return buffer;
		}
	}
	
	public static String encodeBlowFish(String original, boolean isRegister) {
		try {
			byte[] key = KEY.getBytes();
			SecretKeySpec KS = new SecretKeySpec(key, "Blowfish");
			Cipher cipher = null;
			int origLenght = original.getBytes().length;
			byte[] enc = null;

			if (origLenght != 00 && origLenght % 8 == 0) {
				cipher = Cipher.getInstance("Blowfish/CBC/ZeroBytePadding");
				cipher.init(Cipher.ENCRYPT_MODE, KS);
				enc = cipher.doFinal(original.getBytes());
				byte[] tmp = new byte[enc.length - 8];
				for (int i = 0; i < tmp.length; i++) {
					tmp[i] = enc[i];
				}
				enc = tmp;
			} else {
				cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
				cipher.init(Cipher.ENCRYPT_MODE, KS);
				try {
					enc = cipher.doFinal(convertToEncrypt(original));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			String result = Base64
					.encodeToString(enc, Base64.URL_SAFE | Base64.NO_WRAP)
					.replace("+", "-").replace("/", "_").replace("=", ",");
			return result;
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
